package com.sketch.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.commom.enums.DefaultTypeEnum;
import com.sketch.commom.utils.ConversionUtils;
import com.sketch.commom.utils.MapstructUtils;
import com.sketch.entity.Classification;
import com.sketch.entity.DramaSeries;
import com.sketch.entity.SketchInfo;
import com.sketch.entity.TheaterInfo;
import com.sketch.entity.dto.ClassificationDTO;
import com.sketch.entity.dto.DramaSeriesDTO;
import com.sketch.entity.dto.SketchInfoDTO;
import com.sketch.mapper.ClassificationMapper;
import com.sketch.mapper.SketchInfoMapper;
import com.sketch.mapper.TheaterInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassificationService {
    @Resource
    private ClassificationMapper classificationMapper;

    @Resource
    private SketchInfoMapper sketchInfoMapper;

    @Autowired
    private SketchInfoService sketchInfoService;

    public Result addOrUpdate(ClassificationDTO classificationDTO) {
        Classification classification = new Classification();
        BeanUtils.copyProperties(classificationDTO, classification);
        if (ObjectUtils.isEmpty(classificationDTO.getId())) {
            classificationMapper.insert(classification);
        } else {
            classificationMapper.updateById(classification);
        }
        return Result.success();
    }

    public Result list(String type, Integer page, Integer size) {
        LambdaQueryWrapper<Classification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(type), Classification::getType, type);
        Page<Classification> sketchInfoPage = new Page<>(page, size);
        IPage<Classification> result = classificationMapper.selectPage(sketchInfoPage, queryWrapper);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result, dtoPage);
        List<ClassificationDTO> list = new ArrayList<>();
        if (ObjectUtils.isEmpty(result.getRecords())) {
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e -> {
            ClassificationDTO classificationDTO = new ClassificationDTO();
            BeanUtils.copyProperties(e, classificationDTO);
            list.add(classificationDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id) {
        Classification classification = classificationMapper.selectById(id);
        ClassificationDTO classificationDTO = new ClassificationDTO();
        BeanUtils.copyProperties(classification, classificationDTO);
        return Result.success().withData(classificationDTO);
    }

    public Result delete(Long id) {
        classificationMapper.deleteById(id);
        return Result.success();
    }

    public Result<List<ClassificationDTO>> index(String type) {
        type = StringUtils.isBlank(type) ? DefaultTypeEnum.THEME.getDesc() : type;
        LambdaQueryWrapper<Classification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Classification::getType, type);
        List<Classification> classificationList = classificationMapper.selectList(queryWrapper);
        List<ClassificationDTO> classificationDTOList = MapstructUtils.convert(classificationList, ClassificationDTO.class);
        // 查询每个分类有哪些短剧
        if (CollectionUtil.isNotEmpty(classificationList)) {
            // 只需要展示第一个分类的短剧 并且时第一页 时间倒序,其他的页面通过另外的接口查询
            ClassificationDTO classification = classificationDTOList.get(0);
            String type1 = classification.getType();
            LambdaQueryWrapper<SketchInfo> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.like(SketchInfo::getCategoryId, type1).orderByDesc(SketchInfo::getCreateTime);
            Page<SketchInfo> sketchInfoPage = new Page<>(1, 10);
            IPage<SketchInfo> result = sketchInfoMapper.selectPage(sketchInfoPage, queryWrapper1);
            List<SketchInfoDTO> sketchInfoDTOList = MapstructUtils.convert(result.getRecords(), SketchInfoDTO.class);
            if (CollectionUtil.isNotEmpty(sketchInfoDTOList)) {
                for (SketchInfoDTO sketchInfoDTO : sketchInfoDTOList
                ) {
                    sketchInfoDTO.setTagList(sketchInfoService.getTagNameList(sketchInfoDTO.getTagId()));
                }
            }
            classification.setSketchInfoList(sketchInfoDTOList);
        }
        return Result.success(classificationDTOList);
    }

    public Result<List<SketchInfoDTO>> indexType(String type, Integer page, Integer size) {
        LambdaQueryWrapper<SketchInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.like(SketchInfo::getCategoryId, type).orderByDesc(SketchInfo::getCreateTime);
        Page<SketchInfo> sketchInfoPage = new Page<>(page, size);
        IPage<SketchInfo> result = sketchInfoMapper.selectPage(sketchInfoPage, queryWrapper1);
        List<SketchInfoDTO> sketchInfoDTOList = MapstructUtils.convert(result.getRecords(), SketchInfoDTO.class);
        if (CollectionUtil.isNotEmpty(sketchInfoDTOList)) {
            for (SketchInfoDTO sketchInfoDTO : sketchInfoDTOList
            ) {
                sketchInfoDTO.setTagList(sketchInfoService.getTagNameList(sketchInfoDTO.getTagId()));
            }
        }
        return Result.success(sketchInfoDTOList);
    }
}
